package UTS.view;

import UTS.model.Dokumen;
import UTS.model.Dosen;
import UTS.model.Sekretariat;
import UTS.model.Surat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.AncestorEvent;

/**
 *
 * @author Vincentius HMK
 */
public class PanelTerimaSuratDokumen extends JPanel implements ActionListener {

    private ButtonGroup bgPilihan;
    private JRadioButton radioSurat;
    private JRadioButton radioDokumen;
    private JLabel labelNomor;
    private JTextField textNomor;
    private JLabel labelPerihal;
    private JTextField textPerihal;
    private JLabel labelAsal;
    private JTextField textAsal;
    private JLabel labelPenerima;
    private JTextField textPenerima;
    private JLabel labelLetak;
    private JTextField textLetak;
    private JLabel labelTanggalDiterima;
    private JLabel labelFileScanSurat;
    private JButton btnSimpan;
    private JButton btnKeluar;
    private com.toedter.calendar.JDateChooser tanggalDiterima;
    private JLabel labelStatus;
    private JTextField textStatus;

    public PanelTerimaSuratDokumen() {
        setLayout(null);

        radioSurat = new JRadioButton();
        radioSurat.setText("Surat");
        radioSurat.setPreferredSize(new java.awt.Dimension(59, 20));
        radioSurat.setBounds(20, 10, 100, 25);
        this.add(radioSurat);

        radioDokumen = new JRadioButton();
        radioDokumen.setText("Dokumen");
        radioDokumen.setPreferredSize(new java.awt.Dimension(59, 20));
        radioDokumen.setBounds(120, 10, 100, 25);
        this.add(radioDokumen);

        bgPilihan = new ButtonGroup();
        bgPilihan.add(radioSurat);
        bgPilihan.add(radioDokumen);

        labelNomor = new JLabel();
        labelNomor.setText("Nomor");
        labelNomor.setBounds(20, 45, 100, 25);
        this.add(labelNomor);

        textNomor = new JTextField();
        textNomor.setPreferredSize(new java.awt.Dimension(59, 20));
        textNomor.setBounds(130, 45, 300, 25);
        this.add(textNomor);

        labelPerihal = new JLabel();
        labelPerihal.setText("Perihal");
        labelPerihal.setBounds(20, 80, 100, 25);
        this.add(labelPerihal);

        textPerihal = new JTextField();
        textPerihal.setPreferredSize(new java.awt.Dimension(59, 20));
        textPerihal.setBounds(130, 80, 300, 25);
        this.add(textPerihal);

        labelAsal = new JLabel();
        labelAsal.setText("Asal");
        labelAsal.setBounds(20, 115, 100, 25);
        this.add(labelAsal);

        textAsal = new JTextField();
        textAsal.setPreferredSize(new java.awt.Dimension(59, 20));
        textAsal.setBounds(130, 115, 300, 25);
        this.add(textAsal);

        labelPenerima = new JLabel();
        labelPenerima.setText("Penerima");
        labelPenerima.setBounds(20, 150, 100, 25);
        this.add(labelPenerima);

        textPenerima = new JTextField();
        textPenerima.setPreferredSize(new java.awt.Dimension(59, 20));
        textPenerima.setBounds(130, 150, 300, 25);
        this.add(textPenerima);

        labelLetak = new JLabel();
        labelLetak.setText("Letak");
        labelLetak.setBounds(20, 185, 100, 25);
        this.add(labelLetak);

        textLetak = new JTextField();
        textLetak.setPreferredSize(new java.awt.Dimension(59, 20));
        textLetak.setBounds(130, 185, 300, 25);
        this.add(textLetak);

        labelTanggalDiterima = new JLabel("Tanggal Diterima");
        labelTanggalDiterima.setBounds(20, 220, 120, 25);
        this.add(labelTanggalDiterima);

        tanggalDiterima = new com.toedter.calendar.JDateChooser();
        tanggalDiterima.setBounds(130, 220, 300, 25);
        this.add(tanggalDiterima);

        labelStatus = new JLabel("Status Surat");
        labelStatus.setBounds(20, 255, 100, 25);
        this.add(labelStatus);

        textStatus = new JTextField();
        textStatus.setPreferredSize(new java.awt.Dimension(59, 20));
        textStatus.setBounds(130, 255, 300, 25);
        this.add(textStatus);

        btnSimpan = new JButton("Simpan");
        btnSimpan.setBounds(20, 450, 100, 25);
        this.add(btnSimpan);

        btnSimpan.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (radioSurat.isSelected()) {
                    if ((!textNomor.getText().equals("")) && (!textPerihal.getText().equals("")) && (!textAsal.getText().equals("")) && (!textLetak.getText().equals("")) && (!textPenerima.getText().equals("")) && (!textStatus.getText().equals(""))) {//semua atribut di && dgn source code yg sama dgn ini
                        try {
                            Surat surat = new Surat();
                            surat.setNomor(textNomor.getText());
                            surat.setPerihal(textPerihal.getText());
                            surat.setAsal(textAsal.getText());
                            surat.setPenerima(textPenerima.getText());
                            surat.setLetak(textLetak.getText());

                            if (radioSurat.isSelected()) {
                                surat.setPilihanSuratDokumen(radioSurat.getText());
                            } else if (radioDokumen.isSelected()) {
                                surat.setPilihanSuratDokumen(radioDokumen.getText());
                            }
                            surat.setTanggalDiterima(tanggalDiterima.getDate());
                            surat.setStatusSurat(textStatus.getText());

                            Sekretariat.tambahTabelSurat(surat);
                            Sekretariat.simpanTabelSurat();

                        } catch (Exception ex) {
                            Logger.getLogger(PanelTerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        JOptionPane.showMessageDialog(null, "Data surat telah tersimpan");
                    } else {
                        JOptionPane.showMessageDialog(null, "Semua kolom harus terisi");
                    }
                } else {
                    if ((!textNomor.getText().equals("")) && (!textPerihal.getText().equals("")) && (!textAsal.getText().equals("")) && (!textLetak.getText().equals("")) && (!textPenerima.getText().equals("")) && (!textStatus.getText().equals(""))) {
                        try {
                            Dokumen dokumen = new Dokumen();
                            dokumen.setNomor(textNomor.getText());
                            dokumen.setPerihal(textPerihal.getText());
                            dokumen.setAsal(textAsal.getText());
                            dokumen.setPenerima(textPenerima.getText());
                            dokumen.setLetak(textLetak.getText());

                            if (radioSurat.isSelected()) {
                                dokumen.setPilihanSuratDokumen(radioSurat.getText());
                            } else if (radioDokumen.isSelected()) {
                                dokumen.setPilihanSuratDokumen(radioDokumen.getText());
                            }

                            dokumen.setPenerima(textPenerima.getText());
                            dokumen.setTanggalDiterima(tanggalDiterima.getDate());
                            dokumen.setStatusDokumen(textStatus.getText());
//                    surat.setTanggalDiterima(java.util.Date.parse(textTanggalDiterima.getText()));

                            Dosen.tambahTabelDokumen(dokumen);
                            Dosen.simpanTabelDokumen();

                        } catch (Exception ex) {
                            Logger.getLogger(PanelTerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        JOptionPane.showMessageDialog(null, "Data dokumen telah tersimpan");
                    } else {
                        JOptionPane.showMessageDialog(null, "Semua kolom harus terisi");
                    }
                }
            }
        });

        btnKeluar = new JButton("Keluar");
        btnKeluar.setBounds(240, 450, 100, 25);
        this.add(btnKeluar);

        btnKeluar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
